package com.hyt.it.ogt.kq.service.bm.command;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.kq.common.bm.utils.DateUtil;
import com.hyt.it.ogt.kq.common.config.ConfigManager;
import com.hyt.it.ogt.kq.service.bm.feign.pt.PtClient;
import com.hyt.it.ogt.kq.service.bm.feign.pt.model.MessageApiVo;
import com.hyt.it.ogt.kq.service.bm.feign.pt.model.NoticeInfo;
import com.hyt.it.ogt.kq.service.bm.feign.pt.model.SysMessageResultVo;
import com.hyt.loginfo.model.UserVO;
import com.hyt.loginfo.model.param.QueryUsersParam;
import com.hyt.loginfo.service.IBaseLoginService;
import com.hyt.loginfo.service.IUaUserExtService;

import cn.hutool.core.text.StrPool;

@Component
public class PulisherProjectSendMsg2PtService implements Runnable {

	private final Logger log = LoggerFactory.getLogger(PulisherProjectSendMsg2PtService.class);

	private PtClient ptClient;
	private IUaUserExtService iUaUserExtService;
	private ConfigManager configManager;
	private String projectId;
	private String userName;
	private String userId;

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public PulisherProjectSendMsg2PtService(PtClient ptClient, IUaUserExtService iUaUserExtService, IBaseLoginService iBaseLoginService, ConfigManager configManager) {
		super();
		this.ptClient = ptClient;
		this.iUaUserExtService = iUaUserExtService;
		this.configManager = configManager;
	}

	@Override
	public void run() {
		try {
			log.info("SendSysMessage2PtConsumer start");
			ApiResponse<List<SysMessageResultVo>> list = ptClient.querySysMessage("1001");
			List<SysMessageResultVo> dataList = list.getData();
			if (dataList != null && !dataList.isEmpty()) {
				for (int i = 0; i < dataList.size(); i++) {
					List<String> orgids = new ArrayList<String>();
					List<String> roleids = new ArrayList<String>();
					if (StringUtils.isNotBlank(dataList.get(i).getOfficeId())) {
						for (int j = 0; j < dataList.get(i).getOfficeId().split(StrPool.COMMA).length; j++) {
							orgids.add(dataList.get(i).getOfficeId().split(",")[j]);
						}
					}
					if (StringUtils.isNotBlank(dataList.get(i).getRoleId())) {
						for (int j = 0; j < dataList.get(i).getRoleId().split(StrPool.COMMA).length; j++) {
							roleids.add(dataList.get(i).getRoleId().split(StrPool.COMMA)[j]);
						}
					}
					QueryUsersParam param = new QueryUsersParam();
					param.setOrgids(orgids);
					param.setRoleids(roleids);
					ApiResponse<List<UserVO>> userlist = iUaUserExtService.getUsersByRoleIds(param);
					List<UserVO> userList = userlist.getData();
					if (userList != null && userList.size() > 0) {
						if (dataList.get(i).getSendType().contains("1")) {
							List<NoticeInfo> infoList = new ArrayList<NoticeInfo>();
							for (int j = 0; j < userList.size(); j++) {
								NoticeInfo info = new NoticeInfo();
								info.setContent(dataList.get(i).getMessageContent());
								info.setTitle(dataList.get(i).getTitle());
								if ("1".equals(dataList.get(i).getMessageType())) {
									info.setMessageType("2");
								} else if ("2".equals(dataList.get(i).getMessageType())) {
									info.setMessageType("3");
								}
								info.setDealState("0");
								info.setRecieveManId(userList.get(j).getUserid());
								if (StringUtils.isNotBlank(dataList.get(i).getButtonName())) {
									info.setNoticeUrl(configManager.getBmglDetailAddress() + projectId);
								}
								info.setSendMan(userName);
								info.setSendManId(userId);
								info.setRecieveMan(userList.get(j).getUsername());
								info.setSendTimeStr(DateUtil.getStringDate());
								info.setSysMessageId(dataList.get(i).getId());
								info.setOfficeId(userList.get(j).getOfficeid());
								infoList.add(info);
							}
							ptClient.sendNoticeInfo(infoList);
						}
						if (dataList.get(i).getSendType().contains("3") || dataList.get(i).getSendType().contains("2")) {
							List<MessageApiVo> infoList = new ArrayList<MessageApiVo>();
							for (int j = 0; j < userList.size(); j++) {
								if (dataList.get(i).getSendType().contains("2")) {
									if (StringUtils.isNotBlank(userList.get(j).getEmail())) {
										MessageApiVo info = new MessageApiVo();
										info.setContent(dataList.get(i).getMessageContent());
										info.setMessageType("2");
										info.setTitle(dataList.get(i).getTitle());
										if (StringUtils.isNotBlank(dataList.get(i).getButtonName())) {
											info.setMessageUrl(configManager.getBmglDetailAddress() + projectId);
										}
										info.setSendMan(userName);
										info.setSendManId(userId);
										info.setRecieveNumber(userList.get(j).getEmail());
										info.setRecieveManId(userList.get(j).getUserid());
										info.setRecieveMan(userList.get(j).getUsername());
										info.setSendTimeStr(DateUtil.getStringDate());
										info.setOfficeId(userList.get(j).getOfficeid());
										info.setOfficeName(userList.get(j).getOfficename());
										infoList.add(info);
									}
								}
								if (dataList.get(i).getSendType().contains("3")) {
									if (StringUtils.isNotBlank(userList.get(j).getPhone())) {
										MessageApiVo info = new MessageApiVo();
										info.setContent(dataList.get(i).getMessageModeContent());
										info.setMessageType("1");
										info.setTitle(dataList.get(i).getTitle());
										info.setSendMan(userName);
										info.setSendManId(userId);
										if (StringUtils.isNotBlank(dataList.get(i).getButtonName())) {
											info.setMessageUrl(configManager.getBmglDetailAddress() + projectId);
										}
										info.setRecieveNumber(userList.get(j).getPhone());
										info.setRecieveManId(userList.get(j).getUserid());
										info.setRecieveMan(userList.get(j).getUsername());
										info.setSendTimeStr(DateUtil.getStringDate());
										info.setOfficeId(userList.get(j).getOfficeid());
										info.setOfficeName(userList.get(j).getOfficename());
										infoList.add(info);
									}
								}
							}
							ptClient.sendSysMessage(infoList);
						}
					}
				}
				log.info("SendSysMessage2PtConsumer end");
			}
		} catch (Exception e) {
			log.error("## PulisherProjectSendMsg2PtService error", e);
		}
	}
}
